//
//  FLLRegisterViewController.m
//  FLLGodOfCookery
//
//  Created by LL.F on 16/8/29.
//  Copyright © 2016年 LL.F. All rights reserved.
//

#import "FLLRegisterViewController.h"
#import "FLLMacro.h"
#import "UIImageView+WebCache.h"
#import "ZWYNetTool.h"
#import "AFNetworking.h"
#import "FLLSetPasswordViewController.h"
#import "FLLLoginAndRegisterTextField.h"
#import "MBProgressHUD.h"
#import "FLLLoginModel.h"
@interface FLLRegisterViewController ()<UIGestureRecognizerDelegate>
@property (weak, nonatomic) IBOutlet UILabel *getVCAPTCHALabel;
@property (weak, nonatomic) IBOutlet UIImageView *CAPTCHAImageView;
@property (nonatomic, copy) NSString *verifyString;
@property (nonatomic, copy) NSString *sessid;

@property (weak, nonatomic) IBOutlet FLLLoginAndRegisterTextField *phoneNumberTextField;
@property (weak, nonatomic) IBOutlet FLLLoginAndRegisterTextField *messageTextField;
@property (weak, nonatomic) IBOutlet FLLLoginAndRegisterTextField *vertifyTextField;
@property (nonatomic, strong) FLLLoginModel *loginData;
@end

@implementation FLLRegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    
    // 实现隐藏顶部navigationBar也有左滑返回的效果<2>
    self.navigationController.interactivePopGestureRecognizer.delegate = self;
    
    self.getVCAPTCHALabel.layer.borderColor = FLLOrangeColor.CGColor;
    _getVCAPTCHALabel.layer.borderWidth = 1;
    _getVCAPTCHALabel.layer.masksToBounds = YES;
    _getVCAPTCHALabel.layer.cornerRadius = 5.0f;
    
    
}
// 隐藏顶部 navigationBar
// 隐藏顶部 navigationBar后就不会有左滑返回的效果了
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
    // 实现隐藏顶部navigationBar也有左滑返回的效果<3>
    [self.navigationController.view sendSubviewToBack:self.navigationController.navigationBar];
    self.tabBarController.tabBar.hidden = YES;
    
    [self createUserVerifyRequest];
    
}

// 获取图形二维码
- (void)createUserVerifyRequest
{

    [ZWYNetTool POST:@"http://api.izhangchu.com/" andBody:@"methodName=UserVerify&version=4.40&" andBodyStyle: ZWYBodyString andHeader:@{@"Content-Type":@"application/x-www-form-urlencoded"} andResponse:ZWYJSON andSuccessBlock:^(id result) {
       
        self.verifyString = result[@"data"][@"sessid"];
        [self.CAPTCHAImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://m.izhangchu.com/index/verify.html?sessid=%@", [self encodeString:self.verifyString]]] placeholderImage:[UIImage new] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            
        }];
        self.sessid = [self encodeString:self.verifyString];
        

    } andFailureBlock:^(NSError *error) {
        
    }];
    
    
    
}
// 处理图形二维码的方法
-(NSString*)encodeString:(NSString*)unencodedString
{
    
    NSString *encodedString = (NSString *)
    CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,(CFStringRef)unencodedString,NULL,(CFStringRef)@"!*'();:@&=+$,/?%#[]",
    kCFStringEncodingUTF8));
    return encodedString;
    
}


- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.tabBarController.tabBar.hidden = NO;
    self.navigationController.navigationBarHidden = NO;
    // 实现隐藏顶部navigationBar也有左滑返回的效果<4>
    [self.navigationController.view bringSubviewToFront:self.navigationController.navigationBar];
    
}

- (IBAction)didClickedBackToLoginButton:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (IBAction)didClickedExchangeButton:(id)sender
{
   
    [self createUserVerifyRequest];

}
// 跳转到下一页
- (IBAction)didClickedNextButton:(id)sender
{
    
    
    [self createRegisterRequest];
    
    
}

// 获取短信验证码的手势
- (IBAction)getMessage:(id)sender
{
    [self createMessageRequest];
}
// 获取短信的网络请求
- (void)createMessageRequest
{
    

    NSString *bodyString = [NSString stringWithFormat:@"sessid=%@&device_id=190e35f7e04db0d27cf&methodName=UserLogin&verify=%@&mobile=%@&version=4.40&", _sessid, self.vertifyTextField.text, self.phoneNumberTextField.text];
    [ZWYNetTool POST:@"http://api.izhangchu.com/" andBody:bodyString andBodyStyle: ZWYBodyString andHeader:@{@"Content-Type":@"application/x-www-form-urlencoded"} andResponse:ZWYJSON andSuccessBlock:^(id result) {
        
        NSLog(@"短信已发送, 请注意查收");
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        hud.mode = MBProgressHUDModeText;
        hud.label.text =@"短信已发送, 请注意查收";
        [hud hideAnimated:YES afterDelay:0.5f];
        
    } andFailureBlock:^(NSError *error) {
        NSLog(@"请输入正确的验证码");
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        hud.mode = MBProgressHUDModeText;
        hud.label.text =@"请输入正确的验证码";
        [hud hideAnimated:YES afterDelay:0.5f];
    }];

}
// 点击注册的网络请求
- (void)createRegisterRequest
{

    NSString *bodyString = [NSString stringWithFormat:@"code=%@&methodName=UserAuth&mobile=%@&version=4.40&",  self.messageTextField.text, self.phoneNumberTextField.text];
    [ZWYNetTool POST:@"http://api.izhangchu.com/" andBody:bodyString andBodyStyle: ZWYBodyString andHeader:@{@"Content-Type":@"application/x-www-form-urlencoded"} andResponse:ZWYJSON andSuccessBlock:^(id result) {
        
        NSLog(@"注册成功");
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        hud.mode = MBProgressHUDModeText;
        hud.label.text =@"注册成功";
        [hud hideAnimated:YES afterDelay:0.5f];
        // 传值
        self.loginData = [FLLLoginModel modelWithDic:result];
        FLLSetPasswordViewController *setPassword = [[FLLSetPasswordViewController alloc] init];
        setPassword.loginData = self.loginData;
        [self.navigationController pushViewController:setPassword animated:YES];
        
    } andFailureBlock:^(NSError *error) {
        
        NSLog(@"您输入的图形验证码错误");
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        hud.mode = MBProgressHUDModeText;
        hud.label.text =@"您输入的图形验证码错误";
        [hud hideAnimated:YES afterDelay:0.5f];
    }];

    
}
//// 隐藏底部 tabBar
//- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
//{
//    
//    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
//    if (self) {
//        self.hidesBottomBarWhenPushed = YES;
//    }
//    return self;
//}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
